package mvc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mvc.form.ActionForm;
import mvc.form.ExeAction;
import mvc.unti.FullBean;

public class ActionServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ActionServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
//		System.out.println("sign="+request.getParameter("sign"));
//		LoginForm form =(LoginForm) FullBean.full(request);
		ActionForm form = FullBean.full(request);
		String sign = request.getParameter("sign");
		ExeAction exeAction = null;
		Map<String,String> map = ActionMapping.getMap();
		String actionName = map.get(request.getParameter("sign"));
		try {
			Class clazz = Class.forName(actionName);
			exeAction = (ExeAction) clazz.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String message = exeAction.execute(form);
		PrintWriter out = response.getWriter();
		out.print(message);
		out.flush();
		out.close();
		System.out.println(form);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
